<?php

class Gallery_model extends Model {
	
	private $table 	= 'gallerys';
	
	function Gallery_model() {
		parent::Model ();
	}
	
	function add_files( $array ) {
		
		$data = array(
			'cat_id'		=> $array['cat_id'],
			'description'	=> '',
			'name'		=> $array['name'],
			'file_type'	=> $array['file_type'],
			'size'		=> $array['size'],
			'filename'	=> $array['filename'],
			'date'		=> time()
		);
		
		$this->db->insert( $this->table, $data );
	}
	
	function list_files( $cat_id = 0, $num , $offset, &$total ) {
		$data = array ();
		$total = 0;		
		
		if ( $cat_id == 0 ) 
			$where = '';
		else 
			$where = 'WHERE `cat_id` = '.$cat_id;
		
		$query = $this->db->query("SELECT `id`, `cat_id`, `description`, `name`, `file_type`, `size`, `filename` FROM `$this->table` $where ORDER BY `id` DESC LIMIT $offset, $num");
		
		if ( $query->num_rows () > 0 ) 
			$data = $query->result_array();		
		
		// Tông số bản ghi
		$query = $this->db->query("SELECT `id` FROM $this->table $where ");
		$total = $query->num_rows();
		
		$query->free_result ();
		
		return $data;
	}
	
	function select_data ( $id = 0) {
		$data = array ();
		
		if ( is_numeric( $id ) ) {
			$query = $this->db->query("SELECT `id`, `name`, `description`, `filename` FROM `$this->table` WHERE `id` = '$id' LIMIT 1 ");
			
			if ( $query->num_rows () > 0 ) 
				$data = $query->row_array();
			
			$query->free_result ();
		}
		return $data;
	}
	
	function update_info() {
		$id = (int)$_POST['hid'];
		
		$data = array(
			'name'			=> htmlspecialchars( $_POST['name'] ),
			'description'	=> htmlspecialchars( $_POST['description'] )			
		);
				
		$this->db->where('id', $id );
		
		$this->db->update( $this->table, $data);
	}
	
	function del_info( $id) {
		$this->db->where('id', $id );
		$this->db->delete( $this->table );
	}
}
?>